package com.xszx.service;

import com.xszx.dao.GoodsDao;
import com.xszx.entity.Goods;
import com.xszx.entity.MonthCount;
import com.xszx.entity.dto.goods.GoodsQuerRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsService {

    @Resource
    private GoodsDao goodsDao;

    /**
     * 分页查询货物信息
     *
     * @param goodsQuerRequest 货物查询请求的参数
     * @return 查询货物信息列表
     */
    public List<Goods> getGoodsList(GoodsQuerRequest goodsQuerRequest) {
        Integer gid = goodsQuerRequest.getGid();
        String name = goodsQuerRequest.getName();
        String type = goodsQuerRequest.getType();
        String code = goodsQuerRequest.getCode();
        String state = goodsQuerRequest.getState();
        String rCode = goodsQuerRequest.getRCode();
        List<Goods> goodsList = goodsDao.getGoodsQueryWrapper(gid,name,type,code,state,rCode);
        // 如果查到的数据的 iniNum、nowNum 是 null，一般是没开启Mybatis的驼峰命名规则，解决办法是在yml文件中添加配置，或者使用resultMap
        return goodsList;
    }

    /**
     * 添加货物
     *
     * @param goods 货物
     * @return true or false
     */
    public Boolean addGoods(Goods goods) {
        Integer result = goodsDao.insertGoods(goods);
        return result != null && result >= 1;
    }

    /**
     * 根据id删除货物
     *
     * @param gid 货物id
     * @return true or false
     */
    public Boolean deleteGoodsById(Integer gid) {
        Integer result = goodsDao.removeGoodsById(gid);
        return result != null && result >= 1;
    }

    /**
     * 更新货物
     *
     * @param goods 货物
     * @return true or false
     */
    public Boolean updateGoodsById(Goods goods) {
        Integer result = goodsDao.updateGoodsById(goods);
        return result != null && result >= 1;
    }

    public Integer getGoodsCount(){
        return goodsDao.getGoodsCount();
    }

    public double getGoodsGrend() {
        double growthRate = 0;
        // 从数据库获取按月统计结果
        List<MonthCount> monthCounts = goodsDao.countByYearMonth();
        // 转换为Map // 存储每月货物数量
        Map<String, Integer> monthlyCounts = new HashMap<>();
        for (MonthCount mc : monthCounts) {
            monthlyCounts.put(mc.getYearMonth(), mc.getNums());
        }
        // 按月排序并计算环比
        List<String> sortedMonths = new ArrayList<>(monthlyCounts.keySet());
        Collections.sort(sortedMonths);
        for (int i = 1; i < sortedMonths.size(); i++) {
            String currentMonth = sortedMonths.get(i);
            String prevMonth = sortedMonths.get(i - 1);
            int currentCount = monthlyCounts.get(currentMonth);
            int prevCount = monthlyCounts.get(prevMonth);
            growthRate = ((double) currentCount / prevCount - 1) * 100;
            System.out.printf("%s月 vs %s月: 数量 %d vs %d, 环比增长: %.2f%%%n",
                    currentMonth, prevMonth, currentCount, prevCount, growthRate);
        }
        return growthRate;
    }

    //仓库对应的货物信息
    public List<Goods> getGoodsInfo(Integer wid) {
        return goodsDao.getGoodsInfo(wid);
    }
}
